<template>
  <div class="common-modal-overlay" @click.self="handleOverlayClick">
    <div
      class="common-modal glass-effect"
      :class="[`modal-${type}`, sizeClass]"
    >
      <!-- 模态框头部 -->
      <div class="modal-header">
        <div class="header-content">
          <div v-if="icon" class="modal-icon">{{ icon }}</div>
          <h3 class="modal-title">{{ title }}</h3>
        </div>
        <button v-if="closable" @click="handleClose" class="close-button">
          <svg
            width="24"
            height="24"
            viewBox="0 0 24 24"
            fill="none"
            stroke="currentColor"
            stroke-width="2"
          >
            <path d="M18 6L6 18M6 6l12 12" />
          </svg>
        </button>
      </div>

      <!-- 模态框内容 -->
      <div class="modal-content">
        <div v-if="message" class="modal-message">{{ message }}</div>
        <div v-if="$slots.default" class="modal-custom-content">
          <slot></slot>
        </div>
      </div>

      <!-- 模态框底部按钮 -->
      <div v-if="showFooter" class="modal-footer">
        <button
          v-if="showCancelButton"
          @click="handleCancel"
          class="modal-button cancel-button"
          :disabled="loading"
        >
          {{ cancelButtonText }}
        </button>
        <button
          v-if="showConfirmButton"
          @click="handleConfirm"
          class="modal-button confirm-button"
          :class="confirmButtonClass"
          :disabled="loading"
        >
          <span v-if="loading" class="loading-spinner"></span>
          {{ confirmButtonText }}
        </button>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { computed, onMounted, onUnmounted } from "vue";

export interface CommonModalProps {
  // 基础配置
  type?: "info" | "success" | "warning" | "error" | "confirm";
  title?: string;
  message?: string;
  size?: "small" | "medium" | "large";

  // 按钮配置
  showConfirmButton?: boolean;
  showCancelButton?: boolean;
  confirmButtonText?: string;
  cancelButtonText?: string;

  // 行为配置
  closable?: boolean;
  closeOnOverlayClick?: boolean;
  showFooter?: boolean;
  loading?: boolean;

  // 事件
  onConfirm?: () => void | Promise<void>;
  onCancel?: () => void;
  onClose?: () => void;
}

const props = withDefaults(defineProps<CommonModalProps>(), {
  type: "info",
  title: "",
  message: "",
  size: "medium",
  showConfirmButton: true,
  showCancelButton: false,
  confirmButtonText: "确认",
  cancelButtonText: "取消",
  closable: true,
  closeOnOverlayClick: true,
  showFooter: true,
  loading: false,
});

const emit = defineEmits<{
  confirm: [];
  cancel: [];
  close: [];
}>();

// 计算属性
const icon = computed(() => {
  const iconMap = {
    info: "ℹ️",
    success: "✅",
    warning: "⚠️",
    error: "❌",
    confirm: "❓",
  };
  return iconMap[props.type];
});

const sizeClass = computed(() => `modal-${props.size}`);

const confirmButtonClass = computed(() => `button-${props.type}`);

// 事件处理
const handleConfirm = async () => {
  if (props.onConfirm) {
    try {
      await props.onConfirm();
    } catch (error) {
      console.error("Modal confirm action failed:", error);
      return;
    }
  }
  emit("confirm");
};

const handleCancel = () => {
  if (props.onCancel) {
    props.onCancel();
  }
  emit("cancel");
};

const handleClose = () => {
  if (props.onClose) {
    props.onClose();
  }
  emit("close");
};

const handleOverlayClick = () => {
  if (props.closeOnOverlayClick) {
    handleClose();
  }
};

// 键盘事件处理
const handleKeyDown = (event: KeyboardEvent) => {
  if (event.key === "Escape" && props.closable) {
    handleClose();
  }
  if (event.key === "Enter" && props.showConfirmButton && !props.loading) {
    handleConfirm();
  }
};

onMounted(() => {
  document.addEventListener("keydown", handleKeyDown);
});

onUnmounted(() => {
  document.removeEventListener("keydown", handleKeyDown);
});
</script>

<style scoped lang="scss">
.common-modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9999;
  backdrop-filter: blur(4px);
}

.common-modal {
  background: var(--el-bg-color);
  border-radius: var(--el-border-radius-base);
  box-shadow: var(--el-box-shadow-light);
  max-width: 90vw;
  max-height: 90vh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  animation: modalSlideIn 0.3s ease-out;

  &.modal-small {
    width: 400px;
    min-width: 320px;
  }

  &.modal-medium {
    width: 500px;
    min-width: 400px;
  }

  &.modal-large {
    width: 600px;
    min-width: 500px;
  }
}

@keyframes modalSlideIn {
  from {
    opacity: 0;
    transform: translateY(-50px) scale(0.95);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px 24px;
  border-bottom: 1px solid var(--el-border-color);
  background: var(--el-bg-color-overlay);
}

.header-content {
  display: flex;
  align-items: center;
  gap: 12px;
  flex: 1;
}

.modal-icon {
  font-size: 24px;
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--el-bg-color-overlay);
  border-radius: 50%;
  flex-shrink: 0;
}

.modal-title {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
  color: var(--el-text-color-primary);
  line-height: 1.4;
}

.close-button {
  width: 32px;
  height: 32px;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: transparent;
  color: var(--el-text-color-secondary);
  border: none;
  border-radius: var(--el-border-radius-small);
  flex-shrink: 0;
  cursor: pointer;
  transition: all 0.3s ease;

  &:hover {
    background: var(--el-bg-color-overlay);
    color: var(--el-text-color-primary);
  }
}

.modal-content {
  flex: 1;
  padding: 24px;
  overflow-y: auto;
  max-height: calc(90vh - 200px);
}

.modal-message {
  font-size: 16px;
  line-height: 1.6;
  color: var(--el-text-color-primary);
  margin: 0;
}

.modal-custom-content {
  margin-top: 16px;
}

.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
  padding: 20px 24px;
  border-top: 1px solid var(--el-border-color);
  background: var(--el-bg-color-overlay);
}

.modal-button {
  min-width: 80px;
  padding: 10px 20px;
  font-size: 14px;
  font-weight: 500;
  border-radius: var(--el-border-radius-small);
  border: none;
  cursor: pointer;
  transition: all 0.2s ease;

  &:disabled {
    opacity: 0.6;
    cursor: not-allowed;
  }

  .loading-spinner {
    width: 16px;
    height: 16px;
    margin-right: 8px;
    border: 2px solid transparent;
    border-top: 2px solid currentColor;
    border-radius: 50%;
    animation: spin 1s linear infinite;
    display: inline-block;
  }
}

.cancel-button {
  background: var(--el-bg-color);
  color: var(--el-text-color-secondary);
  border: 1px solid var(--el-border-color);

  &:hover:not(:disabled) {
    background: var(--el-bg-color-overlay);
    color: var(--el-text-color-primary);
  }
}

.confirm-button {
  color: white;

  &.button-info {
    background: var(--el-color-primary);

    &:hover:not(:disabled) {
      background: var(--el-color-primary-light-3);
    }
  }

  &.button-success {
    background: var(--el-color-success);

    &:hover:not(:disabled) {
      background: var(--el-color-success-light-3);
    }
  }

  &.button-warning {
    background: var(--el-color-warning);

    &:hover:not(:disabled) {
      background: var(--el-color-warning-light-3);
    }
  }

  &.button-error {
    background: var(--el-color-danger);

    &:hover:not(:disabled) {
      background: var(--el-color-danger-light-3);
    }
  }

  &.button-confirm {
    background: var(--el-color-primary);

    &:hover:not(:disabled) {
      background: var(--el-color-primary-light-3);
    }
  }
}

@keyframes spin {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}

// 响应式设计
@media (max-width: 768px) {
  .common-modal {
    &.modal-small,
    &.modal-medium,
    &.modal-large {
      width: 95vw;
      min-width: unset;
      max-width: 400px;
    }
  }

  .modal-header {
    padding: 16px 20px;
  }

  .modal-content {
    padding: 20px;
    max-height: calc(90vh - 180px);
  }

  .modal-footer {
    padding: 16px 20px;
    flex-direction: column-reverse;
    gap: 8px;
  }

  .modal-button {
    width: 100%;
  }
}
</style>
